package vn.gq.udv.template;

import java.util.ArrayList;
import java.util.List;

import vn.gq.udv.db.DbAdapter;
import vn.gq.udv.db.items.Category;
import vn.gq.udv.utils.AdmobAdsHandler;
import vn.gq.udv.utils.Bookmark;
import vn.gq.udv.utils.ButtonHelper;
import vn.gq.udv.utils.SQLiteDatabaseVersionHandler;
import vn.gq.udv.utils.SharePreferenceUtil;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.google.ads.AdView;

public class CategoryActivity extends SherlockActivity implements OnTouchListener {

	public static final String PREF_STARTCATEGORYACTIVITY = "pref_startcategoryactivity";
	
	private ListView listView;
	private TextView chapterNotification;
	private List<Category> mCategories = new ArrayList<Category>();
	private ProgressDialog mProgressDlg;
	private Bookmark bookmark;
	private AdmobAdsHandler mAdHandler;
	
	private void setListAdapter(final Runnable run) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				mCategories = new DbAdapter(CategoryActivity.this).getCategories();
				runOnUiThread(run);
			}
		}).start();
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chapter_scr);
		String author = getString(R.string.author_name);
		if (!author.trim().equals(""))
			getSupportActionBar().setSubtitle(author);
		mProgressDlg = ProgressDialog.show(CategoryActivity.this, "", "Đang tải dữ liệu, vui lòng chờ !", true);
		new SQLiteDatabaseVersionHandler(this).checkDBVersionAndUpgrade();
		listView = (ListView) findViewById(R.id.chapterlist);
		if (!GQConst.FINISHED) {
			ViewGroup vgr = (ViewGroup) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_list_footer, null, false);
			listView.addFooterView(vgr);
		}
		chapterNotification = (TextView) findViewById(R.id.tvnotification);
		chapterNotification.setSelected(true);
		bookmark = new Bookmark(CategoryActivity.this, chapterNotification);
		setListAdapter(new Runnable() {
			
			@Override
			public void run() {
				
				if (mCategories.size() == 0) {
					SharePreferenceUtil sharepref = new SharePreferenceUtil(CategoryActivity.this);
					sharepref.putBoolean(PREF_STARTCATEGORYACTIVITY, false);
					ChapterActivity.startActivity(CategoryActivity.this, 0, "");
					finish();
				}
				
				listView.setAdapter(new ChapterItemAdapter(CategoryActivity.this, R.id.text_view, mCategories));
				if (mProgressDlg != null) {
					if (mProgressDlg.isShowing()) mProgressDlg.cancel();
				}
			}
		});
		listView.setAdapter(new ChapterItemAdapter(this, R.id.text_view, mCategories));
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if (id != -1) {
					Category cat = mCategories.get(position);
					bookmark.setCategoryBookmark(cat);
					ChapterActivity.startActivity(CategoryActivity.this, cat.getCatId(), cat.getCatName());
				}
			}
		});
		mAdHandler = new AdmobAdsHandler((AdView) findViewById(R.id.adView));
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	protected void onResume() {
		super.onResume();
		bookmark.showNotificationOnStartUp(true);
		mAdHandler.loadAds();
	}
	
	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		getSupportMenuInflater().inflate(R.menu.index, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case android.R.id.home:
				onBackPressed();
				break;
			case R.id.menu_info:
				new ButtonHelper(CategoryActivity.this).showMoreApp();
				break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private class ChapterItemAdapter extends ArrayAdapter<Category> {

		private Context context;
		private List<Category> mChapterItems;
		
		public ChapterItemAdapter(Context context, int textViewResourceId,
				List<Category> objects) {
			super(context, textViewResourceId, objects);
			this.context = context;
			this.mChapterItems = objects;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = ((LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_list, null, false);
			}
			Category item = this.mChapterItems.get(position);
			((TextView) convertView.findViewById(R.id.text_view)).setText(item.getCatName());
			convertView.setTag(item);
			return convertView;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	@Override
	protected void onStop() {
		mAdHandler.onStop();
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		if (mProgressDlg != null) mProgressDlg.cancel();
		mAdHandler.onDestroy();
		super.onDestroy();
	}
}
